import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { TeamMemberApi } from '#/api/cmt/team/member';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';
import { getRangePickerDefaultProps } from '#/utils';
import { getTeamInfoOptions } from '#/api/cmt/team/info';

export const MEMBER_ROLE_OPTIONS = [{ label: '成员', value: 1 }, { label: '管理员', value: 2 }, { label: '队长', value: 3 }];
export const MEMBER_STATUS_OPTIONS = [{ label: '退出', value: 0 }, { label: '正常', value: 1 }];

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'teamId',
      label: '团队ID',
      rules: 'required',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择团队',
        api: getTeamInfoOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'userId',
      label: '会员ID',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入{field_label}', controlsPosition: 'right', class: '!w-full',
      },
    },
    {
      fieldName: 'personId',
      label: '人员ID',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入{field_label}', controlsPosition: 'right', class: '!w-full',
      },
    },
    {
      fieldName: 'role',
      label: '角色',
      rules: 'required',
      component: 'Select',
      formItemClass: 'col-span-1',
      componentProps: {
        options: MEMBER_ROLE_OPTIONS,
        placeholder: '请选择角色',
      },
    },
    {
      fieldName: 'joinTime',
      label: '加入时间',
      component: 'DatePicker',
      formItemClass: 'col-span-1',
      componentProps: {
        showTime: true,
        placeholder: '请选择加入时间',
      },
    },
    {
      fieldName: 'memberStatus',
      label: '状态',
      component: 'RadioGroup',
      formItemClass: 'col-span-1',
      componentProps: {
        options: MEMBER_STATUS_OPTIONS,
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        clearable: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<CmtTeamMemberApi.TeamMember>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: 'ID',
      minWidth: 120,
    },
    {
      field: 'teamId',
      title: '团队ID',
      minWidth: 120,
    },
    {
      field: 'userId',
      title: '会员ID',
      minWidth: 120,
    },
    {
      field: 'personId',
      title: '人员ID',
      minWidth: 120,
    },
    {
      field: 'role',
      title: '角色',
      minWidth: 120,
    },
    {
      field: 'joinTime',
      title: '加入时间',
      minWidth: 120,
    },
    {
      field: 'memberStatus',
      title: '状态',
      minWidth: 120,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
